package Model;

import java.util.ArrayList;
import java.util.Iterator;


/*
 * DVD model class holds specific information for each DVD.
 * Also holds the information about Copy objects and groups them in an ArrayList.
 */
public class DVD
{
    // fields
    private int id;
    private String title;
    private String artist;
    private String pubDate;
    private ArrayList<Copy> copies;
    
    // single constructor
    public DVD(int id, String title, String artist, String pubDate)
    {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.pubDate = pubDate;
        this.copies = new ArrayList<Copy>();
    }
    
    // getters and setters
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getArtist() {
        return artist;
    }
    
    public void setArtist(String artist) {
        this.artist = artist;
    }
    
    public String getPubDate() {
        return pubDate;
    }
    
    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
    
    // creating a copy and adding it to the list
    public void addCopy(String serialNumber, String purchaseDate, String purchasePrice)
    {
        Copy copy = new Copy(serialNumber, purchaseDate, purchasePrice);
        this.copies.add(copy);
    }
    
    // looking for specific copy and removing it from the list
    public void removeCopy(String serialNumber)
    {
        Iterator<Copy> it = copies.iterator();
        boolean found = false;
        while (!found && it.hasNext()) {
            Copy copy = it.next();
            if (copy.getSerialNumber().equals(serialNumber)) {
                it.remove();
                found = true;
           }
        }
    }
    
    // looking for copy and returning single copy
    public Copy findCopy(String serialNumber) 
    {
        Iterator<Copy> it = copies.iterator();
        boolean found = false;
        Copy copy = new Copy();
        while (!found && it.hasNext()) {
            copy = it.next();
            if (copy.getSerialNumber().equals(serialNumber)) {
                found = true;
            }
        }
        return copy;
    }
    
    // return an ArrayList of copies it holds
    public ArrayList<Copy> getCopies()
    {
        return copies;
    }
    
    // String representation of the information in DVD and also an informatin about each Copy it holds.
    public String toString()
    {
        String dvdCopiesInfo;
        dvdCopiesInfo = " " + " ID " + " | " + " title " + " | " + "  artist " + " | " + " pubDate"  + " \n";
        dvdCopiesInfo += " " + id + " | " + title + " | " + artist + " | " + pubDate + " ";
        for(Copy copy : copies)
        {
            dvdCopiesInfo += copy.toString() + "\n";
        }
        return dvdCopiesInfo;
    }    
}
